﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net.MVC;
using FreedomERP.Models;
using FreedomERP.Services.Bd.Impl;
using FreedomERP.Comm;
using FreedomERP.Models.Bd;
using System.Web.Routing;
using FreedomERP.Services.Inv;
using FreedomERP.Services.Inv.Impl;
using FreedomERP.Services.Bd;
using Ext.Net;
using FreedomERP.Models.Inv;
using Newtonsoft.Json;
using FreedomERP.Comm.Utils;
using FreedomERP.Filter;

namespace FreedomERP.Controllers
{
    [CheckSessionFilterAttribute]
    public class SendOrderController : Controller
    {
        BdAccount account = null;

        IInvSendOrderService sendOrderService { get; set; }
        IInvSendOrderDetailService sendOrderDetailService { get; set; }
        IBdCategoryService categoryService { get; set; }
        IBdProductService productService { get; set; }
        IBdCustomerService customerService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (account == null)
                account = BaseController.GetCurrLoginAccount();

            if (sendOrderService == null)
                sendOrderService = new InvSendOrderServiceImpl();
            if (sendOrderDetailService == null)
                sendOrderDetailService = new InvSendOrderDetailServiceImpl();
            if (categoryService == null)
                categoryService = new BdCategoryServiceImpl();
            if (productService == null)
                productService = new BdProductServiceImpl();
            if (customerService == null)
                customerService = new BdCustomerServiceImpl();

            base.Initialize(requestContext);
        }

        /// <summary>
        /// 添加送货单
        /// </summary>
        /// <returns></returns>
        public ActionResult AddOrder()
        {
            return View();
        }

        /// <summary>
        /// 送货单列表
        /// </summary>
        /// <returns></returns>
        public ActionResult OrderList()
        {
            return View();
        }

        /// <summary>
        /// 获取产品价格类别
        /// </summary>
        /// <returns></returns>
        public AjaxStoreResult getProductPriceType()
        {
            List<BdCategory> categoryList = categoryService.QueryCategoryListByFType(ConstData.CATEGORY_PROD_PRICE_TYPE);

            List<BdCategoryView> listView = new List<BdCategoryView>();

            if (categoryList != null)
            {
                foreach (BdCategory obj in categoryList)
                {
                    BdCategoryView categoryView = new BdCategoryView();
                    categoryView.FID = obj.FId;
                    categoryView.FName = obj.FName;
                    listView.Add(categoryView);
                }
            }

            return new AjaxStoreResult(listView, listView.Count);
        }

        /// <summary>
        /// 获取产品价格类别
        /// </summary>
        /// <returns></returns>
        public AjaxStoreResult getCustomerList()
        {
            string currAccountOrgunitFId = BaseController.GetCurrLoginAccountOrgunitFId();
            int customerCount = 0;
            CustomerParameters customerParameters = new CustomerParameters();
            customerParameters.start = 0;
            customerParameters.limit = int.MaxValue;
            customerParameters.FOrgunitId = currAccountOrgunitFId;
            List<BdCustomerView> customerList = customerService.findCustomerList(customerParameters, out customerCount);
            return new AjaxStoreResult(customerList, customerCount);
        }

        /// <summary>
        /// 获取产品列表（待定）
        /// </summary>
        /// <returns></returns>
        public AjaxStoreResult getProductList(string FType)
        {
            List<ProductInfo> productInfoList = productService.getProductInfoByFType(FType);
            return new AjaxStoreResult(productInfoList, productInfoList.Count);
        }

        /// <summary>
        /// 保存送货单
        /// </summary>
        /// <returns></returns>
        //public AjaxResult saveOrder(string msg)
        //{
        //    AjaxResult response = new AjaxResult();
        //    try
        //    {
        //        string FAccountFId = BaseController.GetCurrLoginAccountFId();
        //        InvSendOrderView sendOrderView = JsonConvert.DeserializeObject<InvSendOrderView>(msg);
        //        InvSendOrder sendOrder = new InvSendOrder();
        //        sendOrder.FId = GuidUtils.getGuid();
        //        //sendOrder.FNumber = sendOrderView.FNumber;
        //        //sendOrder.FName = sendOrderView.FName;
        //        //sendOrder.FCustomerId = sendOrderView.FCustomerId;
        //        //sendOrder.FBillDate = DateTime.Parse(sendOrderView.FBillDate);
        //        //sendOrder.FCreateTime = DateTime.Now;
        //        //sendOrder.FCreatorId = FAccountFId;
        //        //sendOrderService.addSendOrder(sendOrder);
        //        response.Result = sendOrder;
        //    }
        //    catch (Exception e)
        //    {
        //        response.Result = false;
        //        response.ErrorMessage = e.Message;
        //    }
        //    return response;
        //}

        /// <summary>
        /// 保存送货单
        /// </summary>
        /// <param name="FId"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public AjaxFormResult saveOrder(string FId, FormCollection values)
        {
            AjaxFormResult response = new AjaxFormResult();
            bool bResult = true;
            string strMsg = String.Empty;
            string returnFId = "";
            try
            {
                string FNumber = values["FNumber"];
                string FName = values["FName"];
                string FCustomer = values["FCustomer"];
                string FBillDate = values["FBillDate"];
                string FCreateTime = values["FCreateTime"];
                string FCreator = values["FCreator"];
                string FModifyTime = values["FModifyTime"];
                string FModifier = values["FModifier"];
                string FRemark = values["FRemark"];
                string FStatusId = values["FStatus_Value"];
                string FCustomerID = null;
                BdCustomer customer = customerService.getCustomerByFName(FCustomer);
                if (customer != null)
                {
                    FCustomerID = customer.FId;
                }
                string FStatusNumber = categoryService.getCategoryByFId(FStatusId).FNumber;

                if (FId == null || FId.Equals(""))
                {
                    InvSendOrder sendOrder = new InvSendOrder();
                    sendOrder.FId = GuidUtils.getGuid();
                    sendOrder.FName = FName;
                    sendOrder.FNumber = FNumber;
                    sendOrder.FCustomerId = FCustomerID;
                    sendOrder.FBillDate = DateTime.Parse(FBillDate);
                    sendOrder.FCreateTime = DateTime.Now;// 创建时间
                    sendOrder.FCreatorId = BaseController.GetCurrLoginAccountFId();
                    sendOrder.FModifyTime = DateTime.Now;
                    sendOrder.FModifierId = BaseController.GetCurrLoginAccountFId();
                    sendOrder.FRemark = FRemark;
                    sendOrder.FStatusId = FStatusId;

                    sendOrderService.addSendOrder(sendOrder);
                    returnFId = sendOrder.FId;
                }
                else// 更新
                {
                    InvSendOrder sendOrder = new InvSendOrder();
                    sendOrder.FId = FId;
                    sendOrder.FNumber = FNumber;
                    sendOrder.FName = FName;
                    sendOrder.FCustomerId = FCustomerID;
                    sendOrder.FBillDate = DateTime.Parse(FBillDate);
                    sendOrder.FCreateTime = DateTime.Parse(FCreateTime);
                    sendOrder.FCreatorId = BaseController.GetCurrLoginAccountFId();
                    sendOrder.FModifyTime = DateTime.Now;// 修改时间
                    sendOrder.FModifierId = BaseController.GetCurrLoginAccountFId();
                    sendOrder.FRemark = FRemark;
                    sendOrder.FStatusId = FStatusId;
                    if (FStatusNumber == ConstData.CATEGORY_SEND_ORDER_STATUS_APPROVE)
                    {
                        sendOrder.FApproverId = BaseController.GetCurrLoginAccountFId();
                        sendOrder.FApproveTime = DateTime.Now;
                    }
                    sendOrderService.updateSendOrder(sendOrder);
                    returnFId = sendOrder.FId;
                }

                bResult = true;
                strMsg = ConstNoteMessage.SAVE_SUCCESS;
            }
            catch (Exception e)
            {
                bResult = false;
                strMsg = e.Message;
            }

            response.Success = bResult;
            response.ExtraParams["msg"] = strMsg;
            response.ExtraParams["FId"] = returnFId;

            return response;
        }

        /// <summary>
        /// 获取送货单列表
        /// </summary>
        /// <param name="FStartDate">起始日期</param>
        /// <param name="FEndDate">结束日期</param>
        /// <param name="FCustomerName">客户名称</param>
        /// <param name="FOrderNumber">订单号</param>
        /// <returns></returns>
        public AjaxStoreResult getOrderList(string FStartDate, string FEndDate, string FCustomerID, string FOrderNumber, string FStatusId, int start, int limit)
        {
            string currAccountOrgunitFId = BaseController.GetCurrLoginAccountOrgunitFId();

            int total = 0;
            List<InvSendOrder> sendOrderList = null;
            if (!FStartDate.Equals("") || !FEndDate.Equals("") || !FCustomerID.Equals("") || !FOrderNumber.Equals("") || !FStatusId.Equals(""))
            {
                sendOrderList = sendOrderService.getSendOrderList(FStartDate, FEndDate, FCustomerID, FOrderNumber, FStatusId, start, limit, out total, currAccountOrgunitFId);
            }
            else
                sendOrderList = sendOrderService.getSendOrderList(null, null, null, null, null, start, limit, out total, currAccountOrgunitFId);

            List<InvSendOrderView> listView = new List<InvSendOrderView>();

            if (sendOrderList != null)
            {
                InvSendOrderView sendOrderView = null;
                string FCustomer = String.Empty;
                string FStatus = String.Empty;

                foreach (InvSendOrder obj in sendOrderList)
                {
                    //FCustomer = customerService.getCustomerByFId(obj.FCustomerId).FName;
                    //FStatus = categoryService.getCategoryByFId(obj.FStatusId).FName;

                    sendOrderView = new InvSendOrderView();
                    sendOrderView.FId = obj.FId;
                    sendOrderView.FNumber = obj.FNumber;
                    sendOrderView.FCustomerId = obj.FCustomerId;
                    sendOrderView.FCustomer = obj.BdCustomer == null ? "" : obj.BdCustomer.FName;
                    sendOrderView.FName = obj.FName;
                    sendOrderView.FBillDate = obj.FBillDate.ToShortDateString();
                    sendOrderView.FCreateTime = obj.FCreateTime.ToString();
                    sendOrderView.FCreatorId = obj.FCreatorId;
                    sendOrderView.FCreator = obj.BdAccount1 == null ? "" : obj.BdAccount1.FAccount;
                    sendOrderView.FModifyTime = obj.FModifyTime.ToString();
                    sendOrderView.FModifierId = obj.FModifierId;
                    sendOrderView.FModifier = obj.BdAccount2 == null ? "" : obj.BdAccount2.FAccount;
                    sendOrderView.FApproveTime = obj.FApproveTime.ToString();
                    sendOrderView.FApproverId = obj.FApproverId;
                    sendOrderView.FApprover = obj.BdAccount == null ? "" : obj.BdAccount.FAccount;
                    sendOrderView.FRemark = obj.FRemark;
                    sendOrderView.FStatusId = obj.FStatusId;
                    sendOrderView.FStatus = obj.BdCategory == null ? "" : obj.BdCategory.FName;
                    sendOrderView.FCustomerName = obj.FCustomerName;
                    sendOrderView.FCustomerMobile = obj.FCustomerMobile;
                    sendOrderView.FCustomerPhone = obj.FCustomerPhone;
                    sendOrderView.FCustomerAddress = obj.FCustomerAddress;
                    listView.Add(sendOrderView);
                }
            }

            return new AjaxStoreResult(listView, total); ;
        }

        /// <summary>
        /// 根据FParentId获取送货单明细列表
        /// </summary>
        /// <param name="FParentId">送货单编号</param>
        /// <returns></returns>
        public AjaxStoreResult getOrderDetailList(string FParentId)
        {
            List<InvSendOrderDetail> sendOrderDetailList = sendOrderDetailService.getSendOrderDetailListByFParentId(FParentId);

            List<InvSendOrderDetailView> listView = new List<InvSendOrderDetailView>();

            if (sendOrderDetailList != null)
            {
                InvSendOrderDetailView sendOrderDetailView = null;
                //string FProductName = String.Empty;

                foreach (InvSendOrderDetail obj in sendOrderDetailList)
                {
                    //FProductName = productService.getProductByFId(obj.FProductId).FName;

                    sendOrderDetailView = new InvSendOrderDetailView();
                    sendOrderDetailView.FId = obj.FId;
                    sendOrderDetailView.FParentId = obj.FParentId;
                    sendOrderDetailView.FProductId = obj.BdProduct == null ? "" : obj.BdProduct.FId;
                    sendOrderDetailView.FProductNumber = obj.BdProduct == null ? "" : obj.BdProduct.FNumber;
                    sendOrderDetailView.FProductName = obj.BdProduct == null ? "" : obj.BdProduct.FName;
                    sendOrderDetailView.FQty = obj.FQty;
                    sendOrderDetailView.FNumber = obj.FNumber;
                    sendOrderDetailView.FPrice = obj.FPrice;
                    sendOrderDetailView.FAmount = obj.FAmount;
                    sendOrderDetailView.FRemark = obj.FRemark;
                    listView.Add(sendOrderDetailView);
                }
            }

            return new AjaxStoreResult(listView, listView.Count); ;
        }

        /// <summary>
        /// 根据FId获取送货单
        /// </summary>
        /// <param name="FId"></param>
        /// <returns></returns>
        public AjaxStoreResult getOrder(string FId)
        {
            InvSendOrder sendOrder = sendOrderService.getSendOrderByFId(FId);

            List<InvSendOrderView> listView = new List<InvSendOrderView>();

            if (sendOrder != null)
            {
                InvSendOrderView sendOrderView = null;
                string FCustomer = String.Empty;
                string FStatus = String.Empty;

                //FCustomer = customerService.getCustomerByFId(sendOrder.FCustomerId).FName;
                //FStatus = categoryService.getCategoryByFId(sendOrder.FStatusId).FName;

                sendOrderView = new InvSendOrderView();
                sendOrderView.FId = sendOrder.FId;
                sendOrderView.FNumber = sendOrder.FNumber;
                sendOrderView.FCustomerId = sendOrder.FCustomerId;
                sendOrderView.FCustomer = sendOrder.BdCustomer == null ? "" : sendOrder.BdCustomer.FName;
                sendOrderView.FName = sendOrder.FName;
                sendOrderView.FBillDate = sendOrder.FBillDate.ToString();
                sendOrderView.FCreateTime = sendOrder.FCreateTime.ToString();
                sendOrderView.FCreatorId = sendOrder.FCreatorId;
                sendOrderView.FCreator = sendOrder.BdAccount1 == null ? "" : sendOrder.BdAccount1.FAccount;
                sendOrderView.FModifyTime = sendOrder.FModifyTime.ToString();
                sendOrderView.FModifierId = sendOrder.FModifierId;
                sendOrderView.FModifier = sendOrder.BdAccount2 == null ? "" : sendOrder.BdAccount2.FAccount;
                sendOrderView.FApproveTime = sendOrder.FApproveTime.ToString();
                sendOrderView.FApproverId = sendOrder.FApproverId;
                sendOrderView.FApprover = sendOrder.BdAccount == null ? "" : sendOrder.BdAccount.FAccount;
                sendOrderView.FRemark = sendOrder.FRemark;
                sendOrderView.FStatusId = sendOrder.BdCategory == null ? "" : sendOrder.BdCategory.FId;
                sendOrderView.FStatus = sendOrder.BdCategory == null ? "" : sendOrder.BdCategory.FNumber;
                sendOrderView.FCustomerName = sendOrder.FCustomerName;
                sendOrderView.FCustomerMobile = sendOrder.FCustomerMobile;
                sendOrderView.FCustomerPhone = sendOrder.FCustomerPhone;
                sendOrderView.FCustomerAddress = sendOrder.FCustomerAddress;
                listView.Add(sendOrderView);
            }

            return new AjaxStoreResult(listView, listView.Count);
        }

        /// <summary>
        /// 根据FId获取送货单详情
        /// </summary>
        /// <param name="FId"></param>
        /// <returns></returns>
        public AjaxStoreResult getOrderDetail(string FId)
        {
            InvSendOrderDetail sendOrderDetail = sendOrderDetailService.getSendOrderDetailByFId(FId);

            List<InvSendOrderDetailView> listView = new List<InvSendOrderDetailView>();

            if (sendOrderDetail != null)
            {
                InvSendOrderDetailView sendOrderDetailView = null;
                string FProductName = String.Empty;

                FProductName = productService.getProductByFId(sendOrderDetail.FProductId).FName;

                sendOrderDetailView = new InvSendOrderDetailView();
                sendOrderDetailView.FId = sendOrderDetail.FId;
                sendOrderDetailView.FParentId = sendOrderDetail.FParentId;
                sendOrderDetailView.FProductId = sendOrderDetail.FProductId;
                sendOrderDetailView.FProductName = FProductName;
                sendOrderDetailView.FQty = sendOrderDetail.FQty;
                sendOrderDetailView.FNumber = sendOrderDetail.FNumber;
                sendOrderDetailView.FPrice = sendOrderDetail.FPrice;
                sendOrderDetailView.FAmount = sendOrderDetail.FAmount;
                sendOrderDetailView.FRemark = sendOrderDetail.FRemark;
                listView.Add(sendOrderDetailView);
            }

            return new AjaxStoreResult(listView, listView.Count);
        }

        /// <summary>
        /// 根据FId删除送货单
        /// </summary>
        /// <param name="FId"></param>
        /// <returns></returns>
        public AjaxResult delOrder(string FId)
        {
            AjaxResult response = new AjaxResult();
            response.Result = true;
            try
            {
                if (string.Empty.Equals(FId))
                {
                    throw new Exception("请选择要删除的送货单后再删除送货单。");
                }
                sendOrderService.delSendOrder(FId);
            }
            catch (Exception e)
            {
                response.Result = false;
                response.ErrorMessage = e.Message;
            }

            return response;
        }

        /// <summary>
        /// 根据FId删除送货单详情
        /// </summary>
        /// <param name="FId"></param>
        /// <returns></returns>
        public AjaxResult delOrderDetail(string FId)
        {
            AjaxResult response = new AjaxResult();

            try
            {
                InvSendOrderDetail sendOrderDetail = sendOrderDetailService.getSendOrderDetailByFId(FId);
                sendOrderDetailService.delSendOrderDetail(sendOrderDetail);
            }
            catch (Exception e)
            {
                response.ErrorMessage = e.Message;
            }

            return response;
        }

        /// <summary>
        /// 获取送货单状态
        /// </summary>
        /// <returns></returns>
        public AjaxStoreResult getSendOrderStatusList()
        {
            List<BdCategory> categoryList = categoryService.QueryCategoryListByFType(ConstData.CATEGORY_SEND_ORDER_STATUS);

            List<BdCategoryView> listView = new List<BdCategoryView>();

            if (categoryList != null)
            {
                foreach (BdCategory obj in categoryList)
                {
                    BdCategoryView categoryView = new BdCategoryView();
                    categoryView.FID = obj.FId;
                    categoryView.FName = obj.FName;
                    listView.Add(categoryView);
                }
            }

            return new AjaxStoreResult(listView, listView.Count); ;
        }


        /// <summary>
        /// 获取可以保存的送货单状态
        /// </summary>
        /// <author>陈新杰</author>
        /// <returns></returns>
        public AjaxStoreResult getSendOrderStatusListForSave()
        {
            List<BdCategory> categoryList = categoryService.GetCategoriesByFTypeAndStatus(ConstData.CATEGORY_SEND_ORDER_STATUS, "Y");

            List<BdCategoryView> listView = new List<BdCategoryView>();

            if (categoryList != null)
            {
                foreach (BdCategory obj in categoryList)
                {
                    BdCategoryView categoryView = new BdCategoryView();
                    categoryView.FID = obj.FId;
                    categoryView.FName = obj.FName;
                    listView.Add(categoryView);
                }
            }

            return new AjaxStoreResult(listView, listView.Count); ;
        }

        /// <summary>
        /// 导出送货单信息到excel
        /// </summary>
        /// <author>梁建东</author>
        /// <returns></returns>
        public ActionResult ExportSendOrderToExcel(String FId, String SortDirect)
        {
            AjaxResult response = null;
            string ResultMsg = string.Empty;
            try
            {
                string serverPath = Server.HtmlEncode(Request.PhysicalApplicationPath);
                sendOrderService.ExportSendOrderToExcel(FId, SortDirect, serverPath, Response);
            }
            catch (System.Exception ex)
            {
                response = new AjaxResult();
                response.ErrorMessage = ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 根据FParentId获取送货单明细列表
        /// </summary>
        /// <param name="FParentId">送货单编号</param>
        /// <author>梁建东</author>
        /// <returns></returns>
        public AjaxStoreResult getOrderDetailListNew(string FParentId)
        {
            List<InvSendOrderDetail> sendOrderDetailList = sendOrderDetailService.getSendOrderDetailListByFParentId(FParentId);

            List<InvSendOrderDetailView> listView = new List<InvSendOrderDetailView>();

            if (sendOrderDetailList != null)
            {
                InvSendOrderDetailView sendOrderDetailView = null;
                string FProductName = String.Empty;

                foreach (InvSendOrderDetail obj in sendOrderDetailList)
                {
                    sendOrderDetailView = new InvSendOrderDetailView();
                    sendOrderDetailView.FId = obj.FId;
                    sendOrderDetailView.FParentId = obj.FParentId;
                    sendOrderDetailView.FProductId = obj.BdProduct == null ? "" : obj.BdProduct.FId;
                    sendOrderDetailView.FProductNumber = obj.BdProduct == null ? "" : obj.BdProduct.FNumber;
                    sendOrderDetailView.FProductName = obj.BdProduct == null ? "" : obj.BdProduct.FName;
                    sendOrderDetailView.FProductModelName = obj.BdProduct == null ? "" : (obj.BdProduct.BdCategory == null ? "" : obj.BdProduct.BdCategory.FName);
                    sendOrderDetailView.FProductInventoryQty = obj.BdProduct == null ? "" : (obj.BdProduct.FQty + "");
                    sendOrderDetailView.FQty = obj.FQty;
                    sendOrderDetailView.FNumber = obj.FNumber;
                    sendOrderDetailView.FPrice = obj.FPrice;
                    sendOrderDetailView.FAmount = obj.FAmount;
                    sendOrderDetailView.FRemark = obj.FRemark;
                    listView.Add(sendOrderDetailView);
                }
            }

            return new AjaxStoreResult(listView, listView.Count); ;
        }

        /// <summary>
        /// 保存送货单信息
        /// </summary>
        /// <param name="msg">JSON字符串</param>
        /// <author>梁建东</author>
        /// <returns></returns>
        public AjaxResult SaveSendOrderMsg(string msg)
        {
            AjaxResult response = new AjaxResult();
            try
            {
                string FId = "";
                string FNumber = "";
                string currAccountFId = BaseController.GetCurrLoginAccountFId();
                string currAccountOrgunitFId = BaseController.GetCurrLoginAccountOrgunitFId();

                InvSendOrderView sendOrderView = JsonConvert.DeserializeObject<InvSendOrderView>(msg);
                if (sendOrderView.FCustomerId == "")
                    sendOrderView.FCustomerId = null;
                if (sendOrderView.FId == null || sendOrderView.FId.Trim().Equals(""))
                {
                    int totalCount = 0;
                    IBdCategoryService cateogryService = new BdCategoryServiceImpl();
                    BdCategory categoryStatus = cateogryService.GetCategoryByFNumberAndFType(ConstData.CATEGORY_SEND_ORDER_STATUS_SAVE, ConstData.CATEGORY_SEND_ORDER_STATUS);
                    if (categoryStatus != null)
                    {
                        sendOrderView.FStatusId = categoryStatus.FId;
                    }
                    else
                    {
                        throw new Exception("送订单状态元数据不存在，无法保存送货单。");
                    }
                    sendOrderService.AddSendOrderNew(sendOrderView, currAccountFId, currAccountOrgunitFId, out FId, out FNumber);
                }
                else
                {
                    int totalCount = 0;
                    IBdCategoryService cateogryService = new BdCategoryServiceImpl();
                    BdCategory categoryStatus = cateogryService.GetCategoryByFNumberAndFType(ConstData.CATEGORY_SEND_ORDER_STATUS_SAVE, ConstData.CATEGORY_SEND_ORDER_STATUS);
                    if (categoryStatus != null)
                    {
                        sendOrderView.FStatusId = categoryStatus.FId;
                    }
                    else
                    {
                        throw new Exception("送订单状态元数据不存在，无法保存送货单。");
                    }
                    sendOrderService.ModifySendOrderNew(sendOrderView, currAccountFId);
                    FId = sendOrderView.FId;
                    FNumber = sendOrderView.FNumber;
                }
                response.Result = FId + "_" + FNumber;
            }
            catch (Exception e)
            {
                response.ErrorMessage = e.Message;
            }
            return response;
        }

        /// <summary>
        /// 审核发货单
        /// 作者：梁建东
        /// </summary>
        public AjaxResult ApproveSendOrder(string FId)
        {
            AjaxResult response = new AjaxResult();
            try
            {
                string currAccountFId = BaseController.GetCurrLoginAccountFId();
                sendOrderService.ApproveSendOrder(FId, currAccountFId);
                response.Result = ConstNoteMessage.SEND_ORDER_APPROVE_SUCCESS;

            }
            catch (Exception e)
            {
                response.ErrorMessage = e.Message;
            }
            return response;
        }

        /// <summary>
        /// 保存送货单
        /// </summary>
        /// <param name="FId"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public AjaxFormResult updateSendOrder(string FId, FormCollection values)
        {
            AjaxFormResult response = new AjaxFormResult();
            bool bResult = true;
            string strMsg = String.Empty;
            try
            {
                string FCustomerId = values["FCustomer_Value"];
                if (string.Empty.Equals(FCustomerId))
                    FCustomerId = null;
                string FRemark = values["FRemark"];
                string FStatusId = values["FStatus_Value"];
                string FStatusNumber = categoryService.getCategoryByFId(FStatusId).FNumber;
                string FUpdateAcoountId = BaseController.GetCurrLoginAccountFId();
                if (FId == null || FId.Equals(""))
                {
                    bResult = false;
                    strMsg = "送货单ID不存在，无法修改！";
                }
                else// 更新
                {
                    sendOrderService.UpdateSendOrder(FId, FCustomerId, FRemark, FStatusId, FStatusNumber, BaseController.GetCurrLoginAccountFId());
                }
                response.ExtraParams["msg"] = "保存送货单成功。";

            }
            catch (Exception e)
            {
                bResult = false;
                strMsg = e.Message;
            }

            response.Success = bResult;
            response.ExtraParams["FId"] = FId;

            return response;
        }

        /// <summary>
        /// 添加送货单(格力)
        /// </summary>
        /// <author>梁建东</author>
        /// <returns></returns>
        public ActionResult AddOrderGeLi()
        {
            return View();
        }

        /// <summary>
        /// 审核发货单(格力)
        /// <author>梁建东</author>
        /// </summary>
        public AjaxResult ApproveSendOrderGeLi(string FId)
        {
            AjaxResult response = new AjaxResult();
            try
            {
                string currAccountFId = BaseController.GetCurrLoginAccountFId();
                sendOrderService.ApproveSendOrderGeLi(FId, currAccountFId);
                response.Result = ConstNoteMessage.SEND_ORDER_APPROVE_SUCCESS;
            }
            catch (Exception e)
            {
                response.ErrorMessage = e.Message;
            }
            return response;
        }

        /// <summary>
        /// 导出送货单信息到excel(格力)
        /// </summary>
        /// <author>梁建东</author>
        /// <returns></returns>
        public ActionResult ExportSendOrderToExcelGeLi(String FId, String SortDirect)
        {
            AjaxResult response = null;
            string ResultMsg = string.Empty;
            try
            {
                string serverPath = Server.HtmlEncode(Request.PhysicalApplicationPath);
                sendOrderService.ExportSendOrderToExcelGeLi(FId, SortDirect, serverPath, Response);
            }
            catch (System.Exception ex)
            {
                response = new AjaxResult();
                response.ErrorMessage = ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 送货单列表(格力)
        /// </summary>
        /// <returns></returns>
        public ActionResult OrderListGeLi()
        {
            return View();
        }

        /// <summary>
        /// 保存送货单信息(格力)
        /// </summary>
        /// <param name="msg">JSON字符串</param>
        /// <author>梁建东</author>
        /// <returns></returns>
        public AjaxResult SaveSendOrderMsgGeLi(string msg)
        {
            AjaxResult response = new AjaxResult();
            try
            {
                string FId = "";
                string FNumber = "";
                string currAccountFId = BaseController.GetCurrLoginAccountFId();
                string currAccountOrgunitFId = BaseController.GetCurrLoginAccountOrgunitFId();

                InvSendOrderView sendOrderView = JsonConvert.DeserializeObject<InvSendOrderView>(msg);
                if (sendOrderView.FCustomerId == "")
                    sendOrderView.FCustomerId = null;
                if (sendOrderView.FId == null || sendOrderView.FId.Trim().Equals(""))
                {
                    int totalCount = 0;
                    IBdCategoryService cateogryService = new BdCategoryServiceImpl();
                    BdCategory categoryStatus = cateogryService.GetCategoryByFNumberAndFType(ConstData.CATEGORY_SEND_ORDER_STATUS_SAVE, ConstData.CATEGORY_SEND_ORDER_STATUS);
                    if (categoryStatus != null)
                    {
                        sendOrderView.FStatusId = categoryStatus.FId;
                    }
                    else
                    {
                        throw new Exception("送订单状态元数据不存在，无法保存送货单。");
                    }
                    sendOrderService.AddSendOrderNewGeLi(sendOrderView, currAccountFId, currAccountOrgunitFId, out FId, out FNumber);
                }
                else
                {
                    int totalCount = 0;
                    IBdCategoryService cateogryService = new BdCategoryServiceImpl();
                    BdCategory categoryStatus = cateogryService.GetCategoryByFNumberAndFType(ConstData.CATEGORY_SEND_ORDER_STATUS_SAVE, ConstData.CATEGORY_SEND_ORDER_STATUS);
                    if (categoryStatus != null)
                    {
                        sendOrderView.FStatusId = categoryStatus.FId;
                    }
                    else
                    {
                        throw new Exception("送订单状态元数据不存在，无法保存送货单。");
                    }
                    sendOrderService.ModifySendOrderNewGeLi(sendOrderView, currAccountFId);
                    FId = sendOrderView.FId;
                    FNumber = sendOrderView.FNumber;
                }
                response.Result = FId + "_" + FNumber;
            }
            catch (Exception e)
            {
                response.ErrorMessage = e.Message;
            }
            return response;
        }

        /// <summary>
        /// 根据FParentId获取送货单明细列表(格力)
        /// </summary>
        /// <param name="FParentId">送货单编号</param>
        /// <returns></returns>
        public AjaxStoreResult getOrderDetailListGeLi(string FParentId)
        {
            List<InvSendOrderDetail> sendOrderDetailList = sendOrderDetailService.getSendOrderDetailListByFParentId(FParentId);

            List<InvSendOrderDetailView> listView = new List<InvSendOrderDetailView>();

            if (sendOrderDetailList != null)
            {
                InvSendOrderDetailView sendOrderDetailView = null;
                //string FProductName = String.Empty;

                foreach (InvSendOrderDetail obj in sendOrderDetailList)
                {
                    //FProductName = productService.getProductByFId(obj.FProductId).FName;

                    sendOrderDetailView = new InvSendOrderDetailView();
                    sendOrderDetailView.FId = obj.FId;
                    sendOrderDetailView.FParentId = obj.FParentId;
                    sendOrderDetailView.FProductId = obj.BdProduct == null ? "" : obj.BdProduct.FId;
                    sendOrderDetailView.FProductNumber = obj.BdProduct == null ? "" : obj.BdProduct.FNumber;
                    sendOrderDetailView.FProductName = obj.BdProduct == null ? "" : obj.BdProduct.FName;
                    sendOrderDetailView.FProductModelName = obj.BdProduct == null ? "" : (obj.BdProduct.BdCategory == null ? "" : obj.BdProduct.BdCategory.FName);
                    sendOrderDetailView.FProductUnitName = obj.BdProduct == null ? "" : (obj.BdProduct.BdCategory2 == null ? "" : obj.BdProduct.BdCategory2.FName);
                    sendOrderDetailView.FQty = obj.FQty;
                    sendOrderDetailView.FNumber = obj.FNumber;
                    sendOrderDetailView.FPrice = obj.FPrice;
                    sendOrderDetailView.FAmount = obj.FAmount;
                    sendOrderDetailView.FRemark = obj.FRemark;
                    listView.Add(sendOrderDetailView);
                }
            }

            return new AjaxStoreResult(listView, listView.Count); ;
        }
    }


}
